package com.gxa.user.recycling.service.impl;


import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.Admin;
import com.gxa.core.recycling.pojo.Score;
import com.gxa.core.recycling.pojo.User;
import com.gxa.core.recycling.service.impl.BaseServiceImpl;
import com.gxa.core.recycling.util.GetTimeUtil;
import com.gxa.user.recycling.mapper.ScoreMapper;
import com.gxa.user.recycling.service.AdminService;
import com.gxa.user.recycling.service.ScoreService;
import com.gxa.user.recycling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author xhy
 * @Date 2021/12/2 10:22
 */
@Service("scoreService")
@Transactional(rollbackFor = {Throwable.class})
public class ScoreServiceImpl extends BaseServiceImpl<Score> implements ScoreService {

@Autowired
    UserService userService;
    @Override
    public Map addSign(Long uid) throws BusinessException {
        Map map =new HashMap();
        Score score = new Score();
        score.setUid(uid);
        score.setSerson("签到");
        score.setChangetime(new Timestamp(System.currentTimeMillis()));
        score.setStatus(true);
        score.setNum(5);
        super.insert(score);
        User user = userService.selectById(uid);
        Integer nowPoints = user.getNowPoints();
        user.setNowPoints(nowPoints+5);
        user.setAllPoints(user.getAllPoints()+5);
        if(user.getState()){
            throw new BusinessException(ExceptionCode.YIJIN_QIANDAO);
        }
        user.setState(true);
        userService.updateById(user);

        Date changetime = score.getChangetime();
        long time = changetime.getTime();
        Date date = GetTimeUtil.initDateByDay();
        long time1 = date.getTime();
        long times=time-time1;
        long Time=86400000-times;
        map.put("Time",score.getChangetime());
        return map;

    }
    @Autowired
    ScoreMapper scoreMapper;

    @Override
    public Long getMaxScoreId(String serson,Long uid) {
        Long maxScoreId = scoreMapper.getMaxScoreId(serson,uid);
        return maxScoreId;
    }
}
